<template>
    <div>
        <Button type="primary" @click="addData">添加</Button>
        <Button type="primary" @click="removeData">删除</Button>
        <Button type="primary" @click="returns">返回</Button>
        <Input v-model="filter.name" placeholder="请输入...">
            <Button slot="append" icon="ios-search" @click="search"></Button>
        </Input>
        
        <Table border :columns="columns1" :data="list" @on-select="selected"></Table>
        <Page :total="filter.total" show-elevator show-total size="small" show-sizer @on-page-size-change="onPageSizeChange" :page-size="10" @on-change="changePage"></Page>

        <Modal
            v-model="modal1"
            title="评论"
            @on-ok="ok"
            @on-cancel="cancel">
            
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
                <FormItem label="姓名" prop="name">
                    <Input v-model="formValidate.name" placeholder="请输入姓名"></Input>
                </FormItem>
                <FormItem label="邮箱" prop="email">
                    <Input v-model="formValidate.email" placeholder="请输入邮箱"></Input>
                </FormItem>
                <FormItem label="标题" prop="title">
                    <Input v-model="formValidate.title" placeholder="请输入标题"></Input>
                </FormItem>
                <FormItem label="内容" prop="text">
                    <quillEditor v-model="formValidate.text" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="请输入..."></quillEditor>             
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
                    <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
                </FormItem>
            </Form>
        </Modal>
    </div>
</template>

<script>
    import Base from '../../common/Base.js';
    import { quillEditor } from 'vue-quill-editor';        
    export default {
        components: {
            quillEditor
        },
        mixins:[Base],
        data () {
            return {
                module:'commend',
                columns1: [
                    {
                        type: 'selection',
                        width: 55,
                        align: 'center',
                    },
                    {
                        type: 'index',
                        width: 55,
                        align: 'center'
                    },
                    {
                        title: '姓名',
                        key: 'name',
                        width:160
                    },
                    {
                        title: '邮箱',
                        key: 'email'
                    },
                    {
                        title: '标题',
                        key: 'title'
                    },
                    {
                        title: '日期',
                        key: 'date'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        width: 120,
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'text',
                                        size: 'small'
                                    },
                                    on:{
                                        click:()=>{
                                            this.editData(params.row._id)
                                        }
                                    }
                                }, '编辑'),
                                h('Button', {
                                    props: {
                                        type: 'text',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(params.row._id);
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                btn:'',
                formValidate: {
                    name: '',
                    email: '',
                    title:'',
                    date: '',
                    text: ''
                },
                ruleValidate: {
                    name: [
                        { required: true, message: '姓名不能为空', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '邮箱不能为空', trigger: 'blur' },
                        { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
                    ],
                    title: [
                        { required: true, message: '标题不能为空', trigger: 'blur' }
                    ],
                    text: [
                        { required: true, message: '请输入内容', trigger: 'blur' },
                        { type: 'string', min: 20, message: '内容不能少于20字', trigger: 'blur' }
                    ]
                }
            }
        },
        methods:{
            returns(){
                this.$router.push({path:'/news'});
            },
            getData(newsId){   //开始显示数据
            // id = this.comId;
            // console.log(this.module)
            // console.log(newsId)
                this.$http({
                    method:'get',
                    url:`http://localhost:3000/commend/list/`,
                    // data:this.filter
                    data:{
                        newsId:newsId
                    }
                }).then(res => {
                    console.log(res)
                    this.list = res.data.rows;
                    this.filter.total = res.data.total;
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            ok () {
                this.$Message.info('点击了确定');
            },
            cancel () {
                this.$Message.info('点击了取消');
            },
            selected(secition){
                // console.log(secition)
                this.ids = secition;
            }
        },
        created(){
            // console.log(this.$route.path.split('/')[2])
            // this.$route.path();
            this.getData(this.$route.path.split('/')[2]);
        }
    }
</script>